package org.wheel.plugins.toolkit.sql.render;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.wheel.module.core.enm.NormalExceptionEnum;
import org.wheel.module.core.exception.NormalException;
import org.wheel.plugins.toolkit.sql.core.AbstractParamRender;
import org.wheel.plugins.toolkit.sql.enums.SqlModel;
import org.wheel.plugins.toolkit.util.sql.SqlConditionBuilder;

import java.util.LinkedHashMap;

public class DirectRender extends AbstractParamRender {

    // 直接使用 LogFactory 创建日志对象，Hutool 会自动检测项目中的日志框架（优先级：Slf4j > Log4j > Logback > JDK Logging）
    private static final Log log = LogFactory.get();

    public DirectRender(LinkedHashMap<String, Object> paramMap) {
        super(paramMap);
    }

    @Override
    public String render(String sql) {
        try {
            SqlConditionBuilder.MergedSqlResult mergedResult = SqlConditionBuilder.mergeSqlAndParameters(
                    sql, paramMap, null, SqlModel.DIRECT_SQL);
            log.debug("SQL: {}, 参数: {}", mergedResult.sql(), mergedResult.params());
            return mergedResult.sql();
        } catch (Exception e) {
//            e.printStackTrace();
            log.error(e);
            throw new NormalException(NormalExceptionEnum.OPERATE_FAIL.getCode(), "SQL无法正确解析: "+sql);
        }
    }
}
